package xyz.chaobei.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.chaobei.entity.OrgPermissionEntity;
import xyz.chaobei.entity.OrgUserEntity;
import xyz.chaobei.entity.OrgUserRoleEntity;
import xyz.chaobei.service.OrgPermissionService;
import xyz.chaobei.service.OrgUserRoleService;
import xyz.chaobei.service.OrgUserService;

import java.util.List;

public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    private OrgUserService orgUserService;

    @Autowired
    private OrgUserRoleService orgUserRoleService;

    @Autowired
    private OrgPermissionService orgPermissionService;

    /***
     * 当访问到页面的时候，链接配置了相应的权限或者 Shiro 标签才会执行此方法否则不会执行
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        OrgUserEntity userInfo = (OrgUserEntity) principals.getPrimaryPrincipal();

        //关联用户的角色信息
        List<OrgUserRoleEntity> roleList = orgUserRoleService.findByUserId(userInfo.getId());

        for (OrgUserRoleEntity role : roleList) {

            //角色关联开通的权限信息
            List<OrgPermissionEntity> permissions = orgPermissionService.findByRoleId(role.getRoleId());

            for (OrgPermissionEntity p : permissions) {
                if (StringUtils.isNotEmpty(p.getPermission())) {
                    authorizationInfo.addStringPermission(p.getPermission());
                }
            }
        }
        return authorizationInfo;
    }

    /***
     * 重写获取用户信息的方法
     * @return
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        System.out.println("MyShiroRealm.doGetAuthenticationInfo()");
        // 获取用户的输入的账号.
        String username = (String) token.getPrincipal();
        System.out.println(token.getCredentials());
        // 通过username从数据库中查找 User对象，如果找到，没找到.
        // 实际项目中，这里可以根据实际情况做缓存，如果不做，Shiro自己也是有时间间隔机制，2分钟内不会重复执行该方法

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username_", username);

        OrgUserEntity userInfo = orgUserService.getOne(wrapper);

        System.out.println("----->>userInfo=" + userInfo);
        if (userInfo == null) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userInfo, // 用户名
                userInfo.getPassword(), // 密码
                ByteSource.Util.bytes(userInfo.getUsername() + userInfo.getSalt()), // salt=username+salt
                getName() // realm name
        );
        return authenticationInfo;

    }

}